*Source: control_function_regression_prepareddata.dta
*Action: Estimating the price instrumenting equation and robustness
*Output: ---

use "control_function_regression_prepareddata.dta", clear

* generate quartiles of income
replace quartile=1 if quartile==. 
tab quartiles, gen(i)

* rescale the price in NAIRA
gen double pt_ = p_/1000

*****************************************
** Instrumentation
*****************************************
gen distoil3 = distoil^3
gen wq9_sq = wq9^2
gen wq9_cu = wq9^3
gen nself_sq = nself^2
gen nself_cu = nself^3
gen remot_sq = remot^2
gen remot_cu = remot^3
gen age_sq = age^2
gen age_cu =age^3

* Adding ndoctors (and higher terms) 
sum ndoctor if no_facility==99
sum ndoctor if no_facility==999
gen ndoctor_sq = ndoctor^2 
gen ndoctor_cu = ndoctor^3 


* Add gender*illness interaction effects
tab typeill, gen(typeill)
foreach var of varlist typeill2-typeill5 i2 i3 i4 {
gen `var'female = `var'*female
}

* wq: weighted quality measure of selected indicators
* Indicators of quality were selected based on the size of their (positive) correlation with price
* Several quality indicators are correlated between themselves. 
* Results remain qualitatively similar and become somewhat more precise when we add more quality predictors, such as the frequency of power cuts, frequency of medicine unavailability, disposal of running water and waiting times. However, we run into convergence problems at the bootstrapping stage of the gender elasticities, as the convergence of the model’s estimation becomes very sensitive to the bootstrapped sample drawn.

gen wq15_sq = wq15^2
gen wq15_cu = wq15^3

gen wq16_sq = wq16^2
gen wq16_cu = wq16^3

gen wq6_sq  = wq6^2
gen wq6_cu  = wq6^3

gen wq17_sq = wq17^2
gen wq17_cu = wq17^3


** distance household to capital city: ilorin 
egen disthhilo = max(remot), by(id)


** indicators = 1 if facility or private doctor
gen isfacility = (no_facility < 99)
gen isprivatedoctor   = (no_facility==99)

* distance village to health facility is assumed zero for private doctors and informal
gen d_village_hf_isprivatedoctor = 0
replace d_village_hf_isprivatedoctor = d_village_hf if no_facility==999 /*private doctor*/

gen  competitor_informal = 0
replace competitor_informal = competitor if no_facility==99 /*informal*/

gen d_village_hf_isinformal = 0
replace d_village_hf_isinformal = d_village_hf if no_facility==99 /*informal*/



****************************************************
* Table 2: Price Instrumenting Equation
****************************************************
* s.e. clustered at the community level
reg  p_  isfacility isprivatedoctor  wq9  wq9_sq wq6 wq6_sq wq17 competitor  d_village_hf_isprivatedoctor female age typeill2-typeill5 i2 i3 i4 typeill2female-typeill5female, clust(q01_10)  
est store phat_8d
predict phat_8d
predict nu_8d, residual

* s.e. clustered at the household level
reg  p_  isfacility isprivatedoctor  wq9  wq9_sq wq6 wq6_sq wq17 competitor  d_village_hf_isprivatedoctor female age typeill2-typeill5 i2 i3 i4 typeill2female-typeill5female, clust(id)  


* AVERAGE MARGINAL EFFECTS

* Competitor's number of doctors 
 gen dyc = _b[wq9]+ 2*_b[wq9_sq]*wq9 
 sum dyc, det 
 drop dyc
 * Competitor's generators
 gen dyc = _b[wq6]+ 2*_b[wq6_sq]*wq6 
 sum dyc, det 
 drop dyc
 * Competitor's number of beds
 gen dyc = _b[wq17]
 sum dyc, det 
 drop dyc


* Response to Associate Editor March 2023: adding more quality predictors
gen wq1_sq = wq1^2
gen wq2_sq = wq2^2
gen wq3_sq = wq3^2
gen wq4_sq = wq4^2
gen wq5_sq = wq5^2
gen wq7_sq = wq7^2

reg  p_  isfacility isprivatedoctor  wq9  wq9_sq wq6 wq6_sq wq17  wq3 wq3_sq wq5 wq5_sq wq4 wq4_sq  competitor  d_village_hf_isprivatedoctor female age typeill2-typeill5 i2 i3 i4 typeill2female-typeill5female, clust(q01_10)  



